package com.jiesen.base.modules.uploadFile.service.imp;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiesen.base.baseDao.BaseServiceImpl;
import com.jiesen.base.modules.uploadFile.dao.FolderDao;
import com.jiesen.base.modules.uploadFile.model.FileAttributes;
import com.jiesen.base.modules.uploadFile.model.Folder;
import com.jiesen.base.modules.uploadFile.service.FileService;
import com.jiesen.base.modules.uploadFile.service.FolderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FolderServiceImp extends BaseServiceImpl<FolderDao,Folder> implements FolderService {

    @Autowired
    private FolderDao folderDao;

    @Autowired
    private FileService fileService;

    @Override
    public int insert(Folder folder) {
        folder.setCreationTime(new Date());
        return folderDao.insert(folder);
    }

    @Override
    public List<Folder> findFolderAll() {
        return folderDao.selectList(null);
    }

    @Override
    public int updateFolderName(Folder folder) {
        return folderDao.updateById(folder);
    }

    @Override
    public int deleteFolderById(Integer id) {
        List<Folder> folders = findFolderSonById(id);
        List<FileAttributes> files = fileService.findByFolderId(id);
        for (FileAttributes file : files) {
            fileService.deleteById(file.getId());
        }
        for (Folder folder : folders) {
            deleteFolderById(folder.getId());
        }
        return folderDao.deleteById(id);
    }

    @Override
    public List<Folder> findFolderSonById(Integer id) {
        QueryWrapper<Folder> wrapper = new QueryWrapper<Folder>();
        wrapper.eq("parent_id",id);
        List<Folder> folders = folderDao.selectList(wrapper);
        return folders;
    }

    @Override
    public Folder findFolderById(Integer id) {
        return folderDao.selectById(id);
    }

    @Override
    public Map findFolderFileById(Integer id) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("folders",findFolderSonById(id));
        map.put("files",fileService.findByFolderId(id));
        return map;
    }

}
